package com.zhiyou100.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.github.pagehelper.PageInfo;
import com.zhiyou100.model.Admin;
import com.zhiyou100.model.Course;
import com.zhiyou100.service.AdminService;
import com.zhiyou100.service.CourseService;
import com.zhiyou100.util.MD5Encryption;
@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService as;
	@Autowired
	private CourseService cs;
	@RequestMapping("/login.do")
	public String adminForward() {
		return "forward:/back/login.jsp";
	}
	
	@RequestMapping("/login")
	public String adminLogin(String username,String password,Integer remember,Integer pageNum,HttpSession session,HttpServletResponse response,Model mo) {
		if(username!=null&&username!=""&&password!=null&&password!="") {
			Admin admin = new Admin();
			admin.setLogin_name(username);
			String login_pwd = "";
			try {
				login_pwd = MD5Encryption.getEncryption(password);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			admin.setLogin_pwd(login_pwd);
			admin = as.selectAdmin(admin);
			admin = as.selectAdmin(admin);
			if(remember!=null) {
				Cookie adminName = new Cookie("login_name", username);
				Cookie adminPwd = new Cookie("login_pwd", password);
				adminName.setPath("/maven_ssm_video");
				adminPwd.setPath("/maven_ssm_video");
				adminName.setMaxAge(195555);
				adminPwd.setMaxAge(195555);
				response.addCookie(adminName);
				response.addCookie(adminPwd);
			}
			if(admin!=null) {
				session.setAttribute("admin", admin);
				return "redirect:/admin/course/manage";
			}
			
		}
		
		return "forward:/back/login.jsp";
	}
	
	@RequestMapping("/logout")
	public String adminLogout(HttpSession session) {
		session.setAttribute("admin", null);
		return "redirect:/back/login.jsp";
	}
}
